export function segmentsIntr(a, b, c, d) {

    // 三角形abc 面积的2倍  
    var area_abc = (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x);

    // 三角形abd 面积的2倍  
    var area_abd = (a.x - d.x) * (b.y - d.y) - (a.y - d.y) * (b.x - d.x);

    // 面积符号相同则两点在线段同侧,不相交 (对点在线段上的情况,本例当作不相交处理);  
    if (area_abc * area_abd >= 0) {
        return false;
    }

    // 三角形cda 面积的2倍  
    var area_cda = (c.x - a.x) * (d.y - a.y) - (c.y - a.y) * (d.x - a.x);
    // 三角形cdb 面积的2倍  
    // 注意: 这里有一个小优化.不需要再用公式计算面积,而是通过已知的三个面积加减得出.  
    var area_cdb = area_cda + area_abc - area_abd;
    if (area_cda * area_cdb >= 0) {
        return false;
    }

    //计算交点坐标  
    var t = area_cda / (area_abd - area_abc);
    var dx = t * (b.x - a.x),
        dy = t * (b.y - a.y);
    return {
        x: a.x + dx,
        y: a.y + dy
    };

}

let __packages = {};
export function namespace(name, o) {
    if (__packages[name]) return;
    __packages[name] = true;
    var p = window,
        strs = name.split('.');
    if (strs.length > 1) {
        for (var i = 0, sz = strs.length - 1; i < sz; i++) {
            var c = p[strs[i]];
            p = c ? c : (p[strs[i]] = {});
        }
    }
    p[strs[strs.length - 1]] || (p[strs[strs.length - 1]] = o || {});
}



function deepCopy(obj, cache = []) {

    function find(list, f) {
        return list.filter(f)[0]
    }

    if (obj === null || typeof obj !== 'object') {
        return obj
    }

    // if obj is hit, it is in circular structure
    const hit = find(cache, c => c.original === obj)
    if (hit) {
        return hit.copy
    }

    const copy = Array.isArray(obj) ? [] : {}
    // put the copy into cache at first
    // because we want to refer it in recursive deepCopy
    cache.push({
        original: obj,
        copy
    })

    Object.keys(obj).forEach(key => {
        copy[key] = deepCopy(obj[key], cache)
    })

    return copy
}